var ExtractTextPlugin = require("extract-text-webpack-plugin")
var HtmlWebpackPlugin = require("html-webpack-plugin")
var autoprefixer = require("autoprefixer")
var path = require("path")
var webpack = require("webpack")

var minimist = require('minimist');
const argv = minimist(process.argv.slice(2))

//判断环境
const env = argv.env ? argv.env : 'dev' //dev,local,test,prod

// 生成相应的 PHP 模板
var htmlWebpackPluginInstances = []

htmlWebpackPluginInstances.push(
  new HtmlWebpackPlugin({
    path: path.resolve('./build/'),
    filename: 'index.html',
    chunk: 'index',
    chunks: ['index'],
    inject: 'body',
    template: path.resolve(`./app/index.html`)
  })
)


// 生成 webpack 所需的 entries 对象
var entries = {}
entries.index = path.resolve('./app/js/entry.js')

module.exports = {
  devtool: false,
  entry: entries,
  output: {
    filename: "js/[name].[chunkhash:8].js",
    publicPath: '',
    chunkFilename: "[name].[chunkhash:8].js"
  },
  resolveLoader: {
    root: path.join(__dirname, 'node_modules'),
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel',
        exclude: /(node_modules|public\/lib)/
      }, {
        test: /\.styl$/,
        loader: ExtractTextPlugin.extract("style-loader", "css-loader!postcss-loader!stylus")
      }, {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract("style-loader", "css-loader!postcss-loader")
      },
      {
        test: /\.swf$/,
        loader: 'url',
        query: {
          // inline files smaller then 10kb as base64 dataURL
          limit: 10,
          // fallback to file-loader with this naming scheme
          name: '[name].[ext]'
        }
      },
      {
        test: /\.(png|jpg|gif)$/,
        loader: 'url',
        query: {
          // inline files smaller then 10kb as base64 dataURL
          limit: 10000,
          // fallback to file-loader with this naming scheme
          name: 'image/[name].[hash:8].[ext]'
        }
      },
      {
        test: /\.(otf|eot|svg|ttf|woff)/,
        loader: 'url',
        query: {
          // inline files smaller then 10kb as base64 dataURL
          limit: 1000,
          // fallback to file-loader with this naming scheme
          name: 'res/[name].[hash:8].[ext]'
        }
      }
    ]
  },
  babel: {
    presets: ['es2015'],
    plugins: ['transform-runtime']
  },
  plugins: htmlWebpackPluginInstances.concat(
    [
      new ExtractTextPlugin('css/[name].[chunkhash:8].css', {
        allChunks: true
      }),
      new webpack.DefinePlugin({
        env:`"${env}"`
      })
    ])

}
